<?php
namespace haibao\cms\business\attendance;
use \haibao\cms\data\attendance\AttendanceUser as AttendanceUserData;
use haibao\frame\data\query\Condition;
use haibao\app\business\BusinessException;
use haibao\cms\business\User;
Class AttendanceUser extends \haibao\cms\business\Base{
	
	public static function operateAttendanceUser($userModel){
		
		$attendanceUser = self::checkAttendanceUserIsExist($userModel->JobNum);
		$data = new AttendanceUserData();
		$attendanceUserModel = new \haibao\cms\model\data\attendance\AttendanceUser();
		if(!$attendanceUser){
			
			$annualLeave = $fullPaidSickLeave = 0;
			if($userModel->EntryTime && $userModel->SocialTime){
				$annualLeave = self::calculationAnnualLeaveBalance($userModel->EntryTime,$userModel->SocialTime);
			}
			if($userModel->EntryTime && !$userModel->IsTrainee ){
				$fullPaidSickLeave = self::calculationFullPaidSickLeaveBalance($userModel->EntryTime,$userModel->SocialTime);
			}
			$attendanceUserModel->AnnualLeave = $annualLeave;
			$attendanceUserModel->FullPaidSickLeave = $fullPaidSickLeave;
			$attendanceUserModel->AnnualHours = $annualLeave;
			$attendanceUserModel->FullPaidSickHours = $fullPaidSickLeave;
			$attendanceUserModel->JobNum = $userModel->JobNum;

			if($userModel->IsRemote == \haibao\cms\model\data\User::IS_REMOTE_YES){
				$attendanceUserModel->Status = \haibao\cms\model\data\attendance\AttendanceUser::STATUS_REMOTE_USER;
			}
			
			if($userModel->IsTrainee == \haibao\cms\model\data\User::IS_TRAINEE_YES){
				$attendanceUserModel->Status = \haibao\cms\model\data\attendance\AttendanceUser::STATUS_TRAINEE;
			}
			
			if($userModel->SocialTime){
				$attendanceUserModel->OneYearSocialDate = date('Y-m-01',mktime(0, 0, 0, date("m",strtotime($userModel->SocialTime)),   '01',   date("Y",strtotime($userModel->SocialTime))+1));
				$attendanceUserModel->TenYearSocialDate = date('Y-m-01',mktime(0, 0, 0, date("m",strtotime($userModel->SocialTime)),   '01',   date("Y",strtotime($userModel->SocialTime))+10));
			}
			$attendanceUserModel->CreateTime = new \DateTime();
			$data->add($attendanceUserModel);
			self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_USER_ADD, $attendanceUserModel);
		}else{
			$arrUpdate = array();
			if($userModel->SocialTime){
				$arrUpdate[\haibao\cms\model\filter\attendance\AttendanceUser::CONDITION_FIELD_NAME_ONE_YEAR_SOCIAL_DATE] = date('Y-m-01',mktime(0, 0, 0, date("m",strtotime($userModel->SocialTime)),   '01',   date("Y",strtotime($userModel->SocialTime))+1));
				$arrUpdate[\haibao\cms\model\filter\attendance\AttendanceUser::CONDITION_FIELD_NAME_TEN_YEAR_SOCIAL_DATE] = date('Y-m-01',mktime(0, 0, 0, date("m",strtotime($userModel->SocialTime)),   '01',   date("Y",strtotime($userModel->SocialTime))+10));
			}
			if($userModel->IsRemote == \haibao\cms\model\data\User::IS_REMOTE_YES){
				$arrUpdate[\haibao\cms\model\filter\attendance\AttendanceUser::CONDITION_FIELD_NAME_STATUS] = \haibao\cms\model\data\attendance\AttendanceUser::STATUS_REMOTE_USER;		
			}
			if($userModel->IsTrainee == \haibao\cms\model\data\User::IS_TRAINEE_YES){
				$arrUpdate[\haibao\cms\model\filter\attendance\AttendanceUser::CONDITION_FIELD_NAME_STATUS] = \haibao\cms\model\data\attendance\AttendanceUser::STATUS_TRAINEE;
			}
			if($arrUpdate){
				$data->updateById($attendanceUser['Id'], $arrUpdate);

				self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_USER_UPDATE, array($attendanceUser['Id'],$arrUpdate));
			}
		}
	}
	
	public static function checkAttendanceUserIsExist($jobNum){
		
		$data = new AttendanceUserData();
		if($attendanceUser = $data->getAttendanceUserByJobNum((int)$jobNum)){
			return $attendanceUser;
		}else{
			return false;
		}
	}
	
	public static function calculationFullPaidSickLeaveBalance($entryTime,$socialTime){
		
		$annualLeave = $fullPaidSickLeave = 0;
		$currentArr = explode('-',date('Y-m-d'));
		$socialArr = explode('-',$socialTime);
		$entryArr = explode('-',$entryTime);
		
		$entryYear = $currentArr[0] - $entryArr[0];

		if($entryYear >=1){
			$fullPaidSickLeave = 6*8;
			
		}elseif($entryYear == 0){
				
			$entryDays = 365 - (strtotime($entryTime) - strtotime(date("Y-01-01")))/86400;
			$temp = explode('.',number_format($entryDays/365*6,1));
			if($temp[1]>=5){
				$fullPaidSickLeave = floatval($temp[0].'.5')*8;
			}else{
				$fullPaidSickLeave = $temp[0]*8;
			}
		}
		return $fullPaidSickLeave;
	}
	
	public static function calculationAnnualLeaveBalance($entryTime,$socialTime){
	
		$annualLeave = $fullPaidSickLeave = 0;
		$currentArr = explode('-',date('Y-m-d'));
		$socialArr = explode('-',$socialTime);
		$entryArr = explode('-',$entryTime);
	
		if($currentArr[1]>$socialArr[1]){
			$socialMonths = abs($currentArr[0] - $socialArr[0]) * 12 + abs($currentArr[1] - $socialArr[1]);
		}else{
			$socialMonths = abs($currentArr[0] - $socialArr[0]) * 12 - abs($currentArr[1] - $socialArr[1]);
		}
	
		$entryYear = $currentArr[0] - $entryArr[0];
	
		if($entryYear >=1){
			if($socialMonths >= 12 && $socialMonths < 120 ){
				$annualLeave = 5*8;
			}elseif($socialMonths >= 120){
				$annualLeave = 10*8;
			}
				
		}elseif($entryYear == 0){
			if($socialMonths >= 12){
	
				$entryDays = 365 - (strtotime($entryTime) - strtotime(date("Y-01-01")))/86400;
				if($socialMonths >= 120){
					$temp = explode('.',number_format($entryDays/365*10,1));
				}else{
					$temp = explode('.',number_format($entryDays/365*5,1));
				}
				if($temp[1]>=5){
					$annualLeave = floatval($temp[0].'.5')*8;
				}else{
					$annualLeave = $temp[0]*8;
				}
			}
		}
		return $annualLeave;
	}
	
	public static function getAttendanceUserByJobNum($jobNum){
		$data = new AttendanceUserData();
		$attendanceUser = $data->getAttendanceUserByJobNum((int)$jobNum);
		$attendanceUserModel = new \haibao\cms\model\data\attendance\AttendanceUser();
		if(!$attendanceUser){
			$attendanceUserModel->AnnualLeave = 0;
			$attendanceUserModel->FullPaidSickLeave = 0;
			$attendanceUserModel->JobNum = $jobNum;
			$attendanceUserModel->CreateTime = new \DateTime();
			$data->add($attendanceUserModel);
		}else{
			$attendanceUserModel->loadArray($attendanceUser);
		}
		return $attendanceUserModel;
	}
	
	public static function getAttendanceUserByfilter($filter){
		$data = new AttendanceUserData();
		return $data->getOne($filter);
	}
	
	public static function subtractUserRequestTime($jobNum,$number,$field){
		$data = new AttendanceUserData();
		$data->subtractUserRequestTime($jobNum, $number, $field);
	}
	
	public static function recoverUserRequestTime($jobNum,$number,$field){
		$data = new AttendanceUserData();
		$data->recoverUserRequestTime($jobNum, $number, $field);
	}
	
	public static function updateAttendanceUserByJobNum($jobNum,$updateArr,$auditUserId=null){
		
		if(!$jobNum){
			throw new BusinessException('工号不能为空',self::DATA_EMPTY);
		}
		$data = new AttendanceUserData();
		$filter = new \haibao\cms\model\filter\attendance\AttendanceUser();
		$filter->where(\haibao\cms\model\filter\attendance\AttendanceUser::CONDITION_FIELD_NAME_JOBNUM,Condition::CONDITION_EQUAL,$jobNum);
		$data->update($updateArr, $filter);
		self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_USER_UPDATE, array($jobNum,$updateArr),$auditUserId);
	}
	public static function getAttendanceUser($filter){
	    $data = new AttendanceUserData();
	    return $data->getOne($filter);
	}
	
	public static  function getAttendanceUserList($filter){
	    $data = new AttendanceUserData();
	    return $data->getAll($filter);
	}
	
	public static function checkFields($model){
	    if(!$model->JobNum){
	        throw new BusinessException('员工工号不能为空',self::DATA_EMPTY);
	    }
	}
	
	public static function addAttendanceUserInfo($model){
	    self::checkOperation(self::FUNC_CODE_ATTENDANCE_USER_ADD);
	    self::checkFields($model);
	    $data = new AttendanceUserData();
	    $data->add($model);
	    self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_USER_ADD, $model);
	}
	
	public static function updateAttendanceUser($id,$arrSet){
	    self::checkOperation(self::FUNC_CODE_ATTENDANCE_USER_UPDATE);
	    $data = new AttendanceUserData();
	    $data->updateById($id, $arrSet);
	    self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_USER_UPDATE, array($id,$arrSet));
	}
	
	public static function delAttenanceUser($id){
	    self::checkOperation(self::FUNC_CODE_ATTENDANCE_USER_DEL);
	    if(!$id){
	        throw new BusinessException('用户Id不能为空',self::DATA_EMPTY_ID);
	    }
	    $data =  new AttendanceUserData();
	    $jobNum = $data->getOneById($id)->JobNum;
	    $userInfo = User::getUserByJobNum($jobNum);
	    
	    $arr = array(\haibao\cms\model\filter\attendance\AttendanceUser::CONDITION_FIELD_NAME_STATUS=>\haibao\cms\model\data\attendance\AttendanceUser::STATUS_QUIT);
	    if($userInfo->IsRemote){
	    	$arr = array(\haibao\cms\model\filter\attendance\AttendanceUser::CONDITION_FIELD_NAME_STATUS=>\haibao\cms\model\data\attendance\AttendanceUser::STATUS_REMOTE_USER);
	    }
	    $data->updateById($id,$arr);
	    self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_USER_DEL, $id);
	}
	
	public static function updateAttendanceUserOverTimeHours(){

		$data =  new AttendanceUserData();
		$data->updateAttendanceUserOverTimeHours();
	}
	
	public static function celarUserCompensationLeave(){
		$data =  new AttendanceUserData();
		$data->celarUserCompensationLeave();
	}
}
